package com.hs.lin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hs.bai.service.IOrderService;
import com.hs.ding.pojo.Dispatch;
import com.hs.ding.pojo.Need;
import com.hs.ding.pojo.Transport;
import com.hs.ding.pojo.Vehicle;
import com.hs.ding.service.IDispatchService;
import com.hs.ding.service.INeedService;
import com.hs.ding.service.ITransportService;
import com.hs.ding.service.IVehicleService;
import com.hs.dto.Constants;
import com.hs.dto.PageBean;
import com.hs.dto.ResultBean;
import com.hs.hu.pojo.Receipt;
import com.hs.hu.service.IReceiptService;
import com.hs.lin.dto.DispatchBean;
import com.hs.lin.mapper.ExceptionRegistrationMapper;
import com.hs.lin.pojo.ExceptionRegistration;
import com.hs.lin.service.IExceptionRegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Lin GongZi
 * @since 2022-06-13
 */
@Service
public class ExceptionRegistrationServiceImpl extends ServiceImpl<ExceptionRegistrationMapper, ExceptionRegistration> implements IExceptionRegistrationService {

    @Autowired
    private ExceptionRegistrationMapper exceptionRegistrationMapper;

    @Autowired
    private IDispatchService dispatchService;


    /**
     * 异常登记方法
     *
     * @param exceptionRegistration
     * @return
     */
    @Override
    public ResultBean registrationEx(ExceptionRegistration exceptionRegistration) {


        //生成异常订单号
        int num = (int) ((Math.random() * 9 + 1) * 100000);
        String exId = String.valueOf(num);
        exceptionRegistration.setId(exId);
        exceptionRegistration.setProcessingProgress("未处理");
        //添加到数据库
        int i = exceptionRegistrationMapper.insert(exceptionRegistration);

        if (i > 0) {
            UpdateWrapper<Dispatch> wrapper = new UpdateWrapper<>();
            wrapper.eq("d_order_id",exceptionRegistration.getcId());
            wrapper.set("e_status",1);
            boolean b = dispatchService.update(wrapper);
            return b ? ResultBean.OK("异常登记成功"):ResultBean.ERROR("异常登记失败");
        }

        return ResultBean.ERROR("异常登记失败");
    }

    @Autowired
    private IOrderService orderService;

    /**
     * 页面展示加分页加条件查询测试
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public ResultBean findPage(Integer currentPage, Integer pageSize, String exId, String oId, String cId, String exceptionType) {
        QueryWrapper<ExceptionRegistration> wrapper = new QueryWrapper();
        if (StringUtils.hasLength(exId) || StringUtils.hasLength(oId) || StringUtils.hasLength(cId) || StringUtils.hasLength(exceptionType)) {
            wrapper.eq("id", exId)
                    .or()
                    .eq("o_id", oId)
                    .or()
                    .eq("c_id", cId)
                    .or()
                    .eq("exception_type", exceptionType);
        }

        //分页查询
        IPage<ExceptionRegistration> p = new Page<>();
        IPage<ExceptionRegistration> page = exceptionRegistrationMapper.selectPage(p, wrapper);
        long total = page.getTotal();
        List<ExceptionRegistration> records = page.getRecords();

        //数据组装
        if (!CollectionUtils.isEmpty(records)) {
            PageBean<ExceptionRegistration> pageBean = new PageBean<>();
            pageBean.setList(records);
            pageBean.setTotal((int) total);
            ResultBean resultBean = new ResultBean();
            resultBean.setCode(Constants.SUCCESS_CODE);
            resultBean.setMsg("查询成功");
            resultBean.setData(pageBean);
            return resultBean;
        }
        return null;
    }

    @Override
    public ExceptionRegistration queryById(String id) {
        return exceptionRegistrationMapper.selectById(id);
    }

    @Autowired
    private IReceiptService receiptService;

    @Autowired
    private IVehicleService vehicleService;

    @Autowired
    private ITransportService transportService;

    @Autowired
    private INeedService needService;


    /**
     * 根据派车单号查询
     *
     * @param id
     * @return
     */
    @Override
    public ResultBean findById(String id,String cId,String oId) {
        //根据派车单查询
        QueryWrapper<Dispatch> wrapper = new QueryWrapper<>();
        wrapper.eq("d_order_id",cId);
        Dispatch dispatch = dispatchService.getOne(wrapper);

        QueryWrapper<Transport> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("t_order_id",dispatch.gettOrderId());
        Transport transport = transportService.getOne(wrapper1);

        QueryWrapper<Vehicle> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("v_order_id",transport.getvOrderId());
        Vehicle vehicle = vehicleService.getOne(wrapper2);

        QueryWrapper<Need> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("order_id",vehicle.getOrderId());
        Need need = needService.getOne(wrapper3);
        System.out.println("need = " + need);

        //查询所有
        DispatchBean dispatchBean = new DispatchBean();
        dispatchBean.setExceptionId(id);
        dispatchBean.setDispatchId(cId);
        dispatchBean.setDispatchOrderId(oId);
        dispatchBean.setDispatchRoute(need.getRoute());
        dispatchBean.setDispatchAppointTime(new Date());
        dispatchBean.setDispatchCarCarrier(transport.getCarrierName());
        dispatchBean.setDispatchCarPlate(transport.getCarPlate());
        dispatchBean.setDispatchCarDriver(transport.getDriverName());

        QueryWrapper<Receipt> wrapper6 = new QueryWrapper<>();
        wrapper6.eq("r_order_id",need.getOrderId());
        Receipt receipt = receiptService.getOne(wrapper6);
        dispatchBean.setRTime(receipt.getrTime());

        List<DispatchBean> dispatchBeans = new ArrayList<>();
        dispatchBeans.add(dispatchBean);
        ResultBean resultBean = new ResultBean();
        resultBean.setData(dispatchBeans);
        resultBean.setCode(Constants.SUCCESS_CODE);
        resultBean.setMsg("okk");

//        DispatchBean dispatchBean = new DispatchBean();
//
//        QueryWrapper<ExceptionRegistration> wrapper = new QueryWrapper<>();
//        wrapper.eq("o_id", id);
//        ExceptionRegistration exceptionRegistration = exceptionRegistrationMapper.selectOne(wrapper);
//
//        dispatchBean.setExceptionId(exceptionRegistration.getId());
//        dispatchBean.setDispatchId(exceptionRegistration.getcId());
//        dispatchBean.setDispatchOrderId(exceptionRegistration.getoId());
//
//        QueryWrapper<Vehicle> qw = new QueryWrapper<>();
//        qw.eq("order_id", id);
//        val vehicle = vehicleService.getOne(qw);
//        QueryWrapper<Transport> qw2 = new QueryWrapper<>();
//        qw2.eq("v_order_id", vehicle.getvOrderId());
//        val transport = transportService.getOne(qw2);
//
//        val need = needService.getById(id);
//        transport.setNeed(need);
//
//        dispatchBean.setDispatchRoute(transport.getNeed().getRoute());
//        dispatchBean.setDispatchAppointTime(new Date());
//        dispatchBean.setDispatchCarCarrier(transport.getCarrierName());
//        dispatchBean.setDispatchCarPlate(transport.getCarPlate());
//        dispatchBean.setDispatchCarDriver(transport.getDriverName());
//
//        QueryWrapper<Receipt> qw3 = new QueryWrapper<>();
//        qw3.eq("r_order_id", id);
//        Receipt receipt = receiptService.getOne(qw3);
//        dispatchBean.setRTime(receipt.getrTime());
//
//        //组装数据
//        ResultBean resultBean = new ResultBean();
//        resultBean.setCode(Constants.SUCCESS_CODE);
//        resultBean.setMsg("ok");
//        List<DispatchBean> dispatchBeans = new ArrayList<>();
//        dispatchBeans.add(dispatchBean);
//        resultBean.setData(dispatchBeans);

//        //查询所有
//        Dispatch dispatchCenter = dispatchService.getById(id);
//
//        //通过客户订单号拿到回单时间
//        QueryWrapper<Receipt> disWrapper = new QueryWrapper<>();
//        disWrapper.eq("r_order_id",dispatchCenter.getDispatchOrderId());
//        disWrapper.eq("r_order_id",dispatchCenter.getTransport().getNeed().getOrderId());
//        Receipt receipt = receiptService.getOne(disWrapper);
//
//        //根据客户订单号拿到异常单号
//        QueryWrapper<ExceptionRegistration> exWrapper = new QueryWrapper();
//        exWrapper.eq("o_id",dispatchCenter.getDispatchOrderId());
//        ExceptionRegistration exceptionRegistration = exceptionRegistrationMapper.selectOne(exWrapper);
//
//        //组装数据
//        DispatchCenterBean dispatchCenterBean = new DispatchCenterBean();
//        dispatchCenterBean.setDispatchId(dispatchCenter.getDispatchId());
//        dispatchCenterBean.setDispatchRoute(dispatchCenter.getDispatchRoute());
//        dispatchCenterBean.setDispatchAppointTime(dispatchCenter.getDispatchAppointTime());
//        dispatchCenterBean.setDispatchCarType(dispatchCenter.getDispatchCarType());
//        dispatchCenterBean.setDispatchCarCarrier(dispatchCenter.getDispatchCarCarrier());
//        dispatchCenterBean.setDispatchCarPlate(dispatchCenter.getDispatchCarPlate());
//        dispatchCenterBean.setDispatchCarDriver(dispatchCenter.getDispatchCarDriver());
//        dispatchCenterBean.setDispatchOrderId(dispatchCenter.getDispatchOrderId());
//
//        dispatchCenterBean.setRTime(receipt.getrTime());
//        dispatchCenterBean.setExceptionId(exceptionRegistration.getId());
//
//        ResultBean resultBean = new ResultBean();
//        resultBean.setCode(Constants.SUCCESS_CODE);
//        resultBean.setMsg("ok");
//
//        List<DispatchCenterBean> dispatchCenterBeans =new ArrayList<>();
//        dispatchCenterBeans.add(dispatchCenterBean);
//        resultBean.setData(dispatchCenterBeans);

        return resultBean;
    }
}
